// packageF/energy/energy.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    isLoadMore: true,
    iPnoneBottomBol: false,
    page: 1,
    totalPage: 0,
    infoEnergy:{}, //能量值的详情
    energyList:[],
    api:'',
    siteName:'',
    imageUrl:''
  },
  loveOn() {
    // wx.navigateTo({
    //   url: '/packageB/member/love/love_list/love_list',
    // })
    wx.navigateTo({
      url:"/packageJ/energy/index"
    })
  },
  energyTab(event) {
    this.setData({
      page:1,
      active:event.detail.name,
      energyList:[]
    })
    if(this.data.active === 0) {
      this.data.api = 'plugin.energy-pool.frontend.controllers.center.energy';
    } else {
      this.data.api = 'plugin.energy-pool.frontend.controllers.center.pool'
    }
    this.energyData();
  },
  energyData() {
    let urlStr = app.getNetAddresss(this.data.api);
    app._postNetWork({
      url: urlStr,
      data:{page:this.data.page,basic_info:1},
      success:  (resdata) => {
        let res = resdata.data;
        let basicInfo =  res.basic_info;
        let list = res.data?.list;
        let info = res.data.info;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = list.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.data.isLoadMore =true;
          this.setData({
            energyList: list?.data,
            infoEnergy:info,
            siteName:basicInfo?.home?.mailInfo?.name,
            imageUrl:basicInfo?.home?.mailInfo?.logo_url,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      if(this.data.active === 0) {
        this.data.api = 'plugin.energy-pool.frontend.controllers.center.energy';
      } else {
        this.data.api = 'plugin.energy-pool.frontend.controllers.center.pool';
      }
      let urlStr = app.getNetAddresss(this.data.api);
      app._getNetWork({
        url: urlStr,
        data: { page: this.data.page },
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data?.list;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              energyList : this.data.energyList.concat(list.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getiPnoneBottom() {
    let that = this;
    try {
      var value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(this.data.active === 0) {
      this.data.api = 'plugin.energy-pool.frontend.controllers.center.energy';
    } else {
      this.data.api = 'plugin.energy-pool.frontend.controllers.center.pool';
    }
    this.energyData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1,
      storeName: ""
    })
    this.getGoodsByShowSales();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageF/energy/energy`
    };
  },
  onShareTimeline() {
    return {
      title: this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageF/energy/energy`
    };
  }
})